* Mechanisms analysis
* 3/9/2025

clear all

program main
	foreach design in sc state triple {
		twfe_regressions, design(`design')
	}
	esttab state sc triple using "$output/twfe_cap_format", replace ///
		keep(1.inter 1.inter#1.capformat 1.inter#2.capformat ///
			1.inter_sc 1.inter_sc#1.capformat 1.inter_sc#2.capformat ///
			1.triple 1.triple#1.capformat 1.triple#2.capformat) se(%5.3f) ///
			stats(N, labels("N") fmt("%9.0fc")) star(* 0.10 ** 0.05 *** 0.01) ///
		order(1.inter 1.inter#2.capformat 1.inter#1.capformat ///
			1.inter_sc 1.inter_sc#2.capformat 1.inter_sc#1.capformat ///
			1.triple 1.triple#2.capformat 1.triple#1.capformat) ///
		varlabels(1.inter "Treated x Post" 1.inter#2.capformat "Treated x Post x Standard" ///
		1.inter#1.capformat "Treated x Post x Auto-enroll" ///
		1.inter_sc "Single x Post" 1.inter_sc#2.capformat "Single x Post x Standard" ///
		1.inter_sc#1.capformat "Single x Post x Auto-enroll" ///
		1.triple "Single x Treated x Post" 1.triple#2.capformat "Single x Treated x Post x Standard" ///
		1.triple#1.capformat "Single x Treated x Post x Auto-enroll") ///
		nomtitles label nonote booktabs
end

program twfe_regressions
	syntax, design(str)
	
	local indiv_controls = "i.sex i.race i.hispan i.edcat age i.diffphys"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	
	use "$for_analysis/`design'_for_regressions_actual_ssi", clear
	bys statefip: gen ever_cap = (!mi(startyear)) // state-level indicator for ever having the CAP
	
	gen capformat = 1 if statefip == 36 | statefip == 25 | statefip == 42	// AE standard CAPs
	replace capformat = 2 if standard == 1 & capformat != 1		// non-AE standard CAPs
	replace capformat = 3 if standard == 0 & ever_cap == 1	// modified CAPs
	replace capformat = 0 if standard == 0 & ever_cap == 0	// no CAP
	
	if "`design'" == "sc" {
		di "sc"
		ren inter inter_sc
		lab var inter_sc "Single x Post"

		reghdfe snap i.inter_sc##ib3.capformat `indiv_controls' ///
			state_unemp perc_democrat_2004 hh_income_2003 `state_snap_controls' ///
			[w = perwt], a(i.hhtype i.year) vce(cl statefip)
		est sto sc
	}
	else if "`design'" == "state" {
		di "state"
		lab var inter "Treated x Post"
		
		* baseline SNAP enrollment in AE states:
		su snap if relyr == -1 & !mi(startyear) & (capformat == 1), d

		reghdfe snap i.inter##ib3.capformat i.statefip `indiv_controls' ///
			state_unemp `state_snap_controls' ///
			[w = perwt], a(i.year) vce(cl statefip)  // regression omits 3 states, one of each CAP format
		est sto state
	}
	else if "`design'" == "triple" {
		di "triple"
		lab var triple "Single x Treated x Post"

		qui reg snap i.triple##ib3.capformat i.hhtype##i.year i.statefip##i.hhtype ///
			i.year##i.statefip `indiv_controls' ///
			[w = perwt], vce(cl statefip)
		est sto triple
	}
end

	
* Execute
main
